<?php

namespace common\models\business;

use Yii;

/**
 * This is the model class for table "business_contract_adjustment".
 *
 * @property integer $id
 * @property integer $contractid
 * @property string $contractname
 * @property string $num
 * @property integer $price
 * @property integer $createtime
 */
class BusinessContractAdjustment extends \yii\db\ActiveRecord
{

    //主要用于日志记录
    public $uselog = [
        'tabcomment' => '账务往来调整',      //用于修改了什么的显示
        'mainfield' => 'contractname',      //主要的显示标题的字段
        'picturefield' => [],        //图片字段
        'timefield' => [],       //时间字段
        'unsetfiled' =>[],      //不需要记录的字段
    ];

    const STATUS_DEFAULT = 0;
    const STATUS_AGREE = 1;
    public static $statues = [
        self::STATUS_DEFAULT => '未审核',
        self::STATUS_AGREE => '已审核',

    ];


    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'business_contract_adjustment';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['contractid','number','price','num'], 'required'],
            [['id', 'contractid', 'num', 'createtime'], 'integer'],
            [['price'], 'number'],
            [['contractname','remark'], 'string', 'max' => 255],
            ['number','string','max'=>50],
            [['input_user'],'string','max'=>20],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'contractid' => '合同',
            'contractname' => '合同名称',
            'num' => '数量',
            'price' => '金额',
            'createtime' => '创建时间',
            'remark' => '备注说明',
            'number' => '调整单号',
            'input_user' => '数据填写人',
            'status' => "状态"
        ];
    }

    public function beforeSave($insert)
    {
        if($this->isNewRecord){
            $this->createtime = time();
            $this->input_user = Yii::$app->user->identity->username;
        }

        $this->contractname = BusinessContract::findOne($this->contractid)->name;
        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }


    /**
     * @return \yii\db\ActiveQuery
     * 获取关联的合同
     */
    public function getContract()
    {
        return $this->hasOne(BusinessContract::className(),['id'=>'contractid']);
    }


    /**
     *
     * 获取关联的结算单
     */
    public function getSta()
    {
        return $this->hasOne(BusinessContractStatements::className(),['id'=>'sta_id']);
    }

}
